<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "front_user".
 *
 * @property string $uid
 * @property string $username
 * @property string $password
 * @property string $mobile
 * @property string $create_time
 * @property integer $status
 * @property integer $manager_id
 * @property string $email
 */
class FrontUser extends \yii\db\ActiveRecord
{
    const  STATUS_YES=1;   //1表示用户正常使用
    const  STATUS_NO=0;    //0表示用户已停用
    public $password2;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'front_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['create_time', 'status', 'manager_id'], 'integer'],
            [['username'], 'string', 'max' => 64],
            [['username'], 'unique'],
            [['username'], 'required', 'message' =>"用户名不能为空"],
            [['password', 'mobile'], 'string', 'max' => 32],
            [['password2'], 'string', 'max' => 32],
            [['password'], 'required', 'message' =>"密码不能为空"],
            [['email'], 'string', 'max' => 128]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'uid' => 'Uid',
            'username' => '用户名',
            'password' => '密码',
            'password2' => '密码',
            'mobile' => '手机号',
            'create_time' => '创建时间',
            'status' => '状态',
            'manager_id' => '后台管理员',
            'email' => '邮箱',
        ];
    }
    public function save($runValidation = true, $attributeNames = null){
           if($this->isNewRecord){
               $this->create_time=time();
               $this->manager_id=yii::$app->session->get('uid');
               $this->password=md5($this->password);
           }else{
               if(!empty($this->password2)){
               $this->password=md5($this->password2);
               }
           }
        return parent::save($runValidation, $attributeNames);
    }
    public function validatePassword($password)
    {
        return $this->password === md5($password);
    }
    public function getDate($time){
        return date("Y-m-d H:i:s",$time);
    }
}
